/*
 ============================================================================
 Name		: FbTopTabgroup.cpp
 Author	  : ceze
 Version	 : 1.0
 Copyright   : FlashBox for ver.Zhongyou(���ʰ�)
 Description : CFbTopTabgroup implementation
 ============================================================================
 */

#include "FbTabgroupTop.h"
#include <EIKENV.H>
#include <stringloader.h>
#include "ConstDefined.hrh"
#include "flashbox4.mbg"
#include "Flashbox4_0x20021B21.rsg"
#include <eiklabel.h>  // for example label control
CFbTabgroupTop::CFbTabgroupTop ()
:m_pTitleText(NULL), m_pSubTitleText(NULL)
{
	// No implementation required
}

CFbTabgroupTop::~CFbTabgroupTop ()
{
	//背景图
	//delete m_pBack;
//	delete m_pTitleLabel;
	delete m_pAppLogo;
	delete m_pTopImage;
	delete m_pBottomImage;
	delete m_pTitleText;
	delete m_pSubTitleText;
}

CFbTabgroupTop* CFbTabgroupTop::NewLC (const TRect& aRect, CCoeControl *pParent )
{
	CFbTabgroupTop* self = new (ELeave) CFbTabgroupTop ();
	CleanupStack::PushL (self );
	self->ConstructL (aRect, pParent );
	return self;
}

CFbTabgroupTop* CFbTabgroupTop::NewL (const TRect& aRect, CCoeControl *pParent )
{
	CFbTabgroupTop* self = CFbTabgroupTop::NewLC (aRect, pParent );
	CleanupStack::Pop (); // self;
	return self;
}

void CFbTabgroupTop::ConstructL (const TRect& aRect, CCoeControl *pParent )
{
	CFbTabgroup::ConstructL (aRect, pParent );

	m_pAppLogo = (CEikonEnv::Static ())->CreateBitmapL (KFlashboxImagePath,
			EMbmFlashbox4Applogo );
	m_pTopImage = (CEikonEnv::Static ())->CreateBitmapL (KFlashboxImagePath,
			EMbmFlashbox4Tabbar1 );
	m_pBottomImage = (CEikonEnv::Static ())->CreateBitmapL (KFlashboxImagePath,
			EMbmFlashbox4Tabbar2 );
	
	/*
	//标题
	m_pTitleLabel = new (ELeave) CEikLabel;
	CleanupStack::PushL (m_pTitleLabel );
	m_pTitleLabel->SetContainerWindowL (*this );
	m_pTitleLabel->SetTextL (KNullDesC );
	m_pTitleLabel->SetRect (KFbTabgroupTextRect );
	// create font
	CFont* normalFont = 0;
	// Create font specification.
	TFontSpec fontSpec = iCoeEnv->NormalFont ()->FontSpecInTwips ();
	//set font's size
	fontSpec.iHeight = (8 * fontSpec.iHeight/10);
	//apply font
	iCoeEnv->ScreenDevice ()->GetNearestFontInTwips (normalFont, fontSpec );
	m_pTitleLabel->SetFont(normalFont);
    iCoeEnv->ScreenDevice()->ReleaseFont(normalFont);
	m_pTitleLabel->OverrideColorL (EColorLabelTextEmphasis, KFbTabgroupTopColor );
	m_pTitleLabel->OverrideColorL (EColorLabelHighlightFullEmphasis,
			KFbTabgroupBottomColor );
	m_pTitleLabel->SetEmphasis (CEikLabel::EFullEmphasis );
	CleanupStack::Pop ();
	*/
	
}

/**
 * From CoeControl,CountComponentControls.
 */
TInt CFbTabgroupTop::CountComponentControls () const
{
//	return 1;//+ m_pTabArray->Count();//m_pTitleLabel + 
	//return 2;
	return 0;
}

/**
 * From CCoeControl,ComponentControl.
 */
CCoeControl* CFbTabgroupTop::ComponentControl (TInt aIndex ) const
{
	//return m_pTitleLabel;
	return NULL;
}

/**
 * From CoeControl,
 * 按键事件接收处理
 * aKeyEvent ：键盘事件。TKeyEvent 类描述了键盘事件的细节，
 * 				他包括四个属性，分别是iCode, iModifiers, iRepeats, iScanCode 
 * aType ：键盘事件类型，包括：EEventKey, EEventKeyUp or EEventKeyDown
 */
TKeyResponse CFbTabgroupTop::OfferKeyEventL (const TKeyEvent& aKeyEvent, TEventCode /*aType*/)
{
	TKeyResponse ret = EKeyWasNotConsumed;
	if(m_isDimmedTabs)
		return ret;
	
	TInt indexTemp = m_activeTabIndex;
	switch (aKeyEvent.iCode )
	{
	case EKeyLeftArrow://左拨
		indexTemp--;
		if (indexTemp < 0 )
		{
			indexTemp = m_pTabArray->Count () - 1;
		}
		break;
	case EKeyRightArrow://右拨
		indexTemp++;
		if (indexTemp >= m_pTabArray->Count () )
		{
			indexTemp = 0;
		}
		break;
	default:
		return ret;
	}
	//激活新tab
	SetActiveTabByIndexL (indexTemp );
	ret = EKeyWasConsumed;
	return ret;
}

/**
 * 添加Tab项
 */
void CFbTabgroupTop::AddTabL (TInt aTabId, const TDesC& aResFileName, const TInt& aResImageId, const TInt& aResImageMaskId, const TInt& aResBackId, const TInt & aResTextId )
{
	if (!m_pTabArray )
	{
		m_pTabArray = new (ELeave) CArrayPtrFlat<CFbTabitem> (5 );
	}
	TInt itemIndex = m_pTabArray->Count ();
	TPoint aPos (itemIndex * (KFbTabitemSize.iWidth) + KFbTabitemPosStart.iX,
			KFbTabitemPosStart.iY );
	TRect aRect (aPos, KFbTabitemSize );
	//创建tab项
	CFbTabitem *pTab = CFbTabitem::NewL (this, aTabId, aResFileName,
			aResImageId, aResImageMaskId, aResBackId, aRect, aResTextId );
	pTab->SetBackColor (KFbTabgroupTopColor );
	//添加到数组中
	m_pTabArray->AppendL (pTab );
}

/**
 * From CCoeControl, Draw
 * Draw this CFbTabgroup to the screen.
 * @param aRect the rectangle of this view that needs updating
 */
void CFbTabgroupTop::Draw (const TRect& /*aRect*/) const
{
	// Get the standard graphics context
	CWindowGc& gc = SystemGc ();
	TRect drawRect (Rect () );
	//gc.SetPenColor (KRgbWhite);
	//gc.SetBrushColor (KFbTabgroupTopColor );
	//gc.SetBrushStyle (CGraphicsContext::ESolidBrush );
	//gc.DrawRect (drawRect );
	if (m_pAppLogo )
	{
		gc.DrawBitmap (KFbAppLogoRect, m_pAppLogo );
	}
	if (m_pTopImage )
	{
		gc.DrawBitmap (KFbAppTabgroupTopRect, m_pTopImage );
	}
	if (m_pBottomImage )
	{
		gc.DrawBitmap (KFbAppTabgroupBottomRect, m_pBottomImage );
	}
	//draw title text and Manhuashuba Sub title!
	
	CFont* font = NULL;
	// Create font specification.
	TFontSpec fontSpec = iCoeEnv->NormalFont()->FontSpecInTwips();
	//set font's size
	fontSpec.iHeight = (3 * fontSpec.iHeight / 4);
	//apply font
	iCoeEnv->ScreenDevice()->GetNearestFontInTwips(font, fontSpec);
	gc.UseFont(font);
	
	if(m_pTitleText)
	{
		gc.SetPenColor(KFbTabgroupTopTitleColor);
		gc.SetPenStyle(CGraphicsContext::ESolidPen);
		gc.DrawText(m_pTitleText->Des(), TPoint(KFbTabgroupTextRect.iTl.iX, KFbTabgroupTextRect.iBr.iY - 1));
	}
	
	if(m_pSubTitleText)
	{
		gc.SetPenColor(KRgbBlack);
		gc.SetPenStyle(CGraphicsContext::ESolidPen);
		gc.DrawText(m_pSubTitleText->Des(), TPoint(KFbTabgroupTextRect.iTl.iX + 80, KFbTabgroupTextRect.iBr.iY - 1));
	}
	
	gc.DiscardFont();
	iCoeEnv->ScreenDevice()->ReleaseFont(font);
}

/**
 * 通过序号激活tab项
 * Hightlight (activate) a tab.
 */
void CFbTabgroupTop::SetActiveTabByIndexL (TInt aTabIndex )
{
	if (aTabIndex < 0 || aTabIndex >= m_pTabArray->Count () )
		return;
	
	if (m_activeTabIndex != aTabIndex )
	{
		//隐退原激活tab
		if (m_activeTabIndex >= 0 && m_activeTabIndex < m_pTabArray->Count () )
		{
			CFbTabitem *pTempTab = static_cast<CFbTabitem*> (m_pTabArray->At (
					m_activeTabIndex ) );
			pTempTab->SetItemActiveL (EFalse );
		}
		//激活新tab
		CFbTabitem *pTempActiveTab = static_cast<CFbTabitem*> (m_pTabArray->At (
				aTabIndex ) );
		pTempActiveTab->SetItemActiveL (ETrue );
		m_activeTabIndex = aTabIndex;
		HBufC* title = pTempActiveTab->TabTitle ();
		/*
		 //使用Label
		if (title )
			m_pTitleLabel->SetTextL (*pTempActiveTab->TabTitle () );
		else
			m_pTitleLabel->SetTextL (KNullDesC );
		*/
		if(m_pTitleText)
		{
			delete m_pTitleText;
			m_pTitleText = NULL;
		}
		if(title)
		{
			m_pTitleText = title->Des().Alloc();
		}
				
		//发送新tab切换消息
		TInt tabId = pTempActiveTab->TabId ();
		m_pTabObserver->TabChangedL (m_activeTabIndex, tabId );
		//刷新背景
		DrawNow ();
	}
}

void CFbTabgroupTop::SetTabsDimmed(TBool aDimmed)
{
	CFbTabgroup::SetTabsDimmed(aDimmed);
	
	if(m_pBottomImage)
		delete m_pBottomImage;	
	if(aDimmed)
	{
		m_pBottomImage = (CEikonEnv::Static ())->CreateBitmapL (KFlashboxImagePath,
						EMbmFlashbox4Tabbar3 );
	}
	else
	{
		m_pBottomImage = (CEikonEnv::Static ())->CreateBitmapL (KFlashboxImagePath,
						EMbmFlashbox4Tabbar2 );
	}
	
	DrawNow();
}

/**
* label text
 */
void CFbTabgroupTop::SetLabelText (const TDesC &aText )
{
	if(m_pTitleText)
	{
		delete m_pTitleText;
		m_pTitleText = NULL;
	}
	m_pTitleText = aText.Alloc();
}

/**
* label text For Manhuashuba Sub title
 */
void CFbTabgroupTop::SetLabel2Text (const TDesC &aText )
{
	if(m_pSubTitleText)
	{
		delete m_pSubTitleText;
		m_pSubTitleText = NULL;
	}
	if(aText.Length() != 0)
		m_pSubTitleText = aText.Alloc();
}


/**
 * from CCoeControl
 */
/*
void CFbTabgroupTop::MakeVisible(TBool aVisible)
{
	CCoeControl::MakeVisible (aVisible );
	if(IsDimmed())
	{
		return;
	}
	else
	{
		
		CFbTabgroup::SetTabsDimmed(!aVisible);
	}

}
*/
